package com.company.阻塞队列;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * @ProjectName: 复习java
 * @Package: com.company.阻塞队列
 * @ClassName: demo2
 * @Author: wym
 * @Date: 2020/9/29 15:29
 * @Version: 1.0
 */
public class demo2 {
    public static void main(String[] args) {
        BlockingQueue<String> blockingQueue=new SynchronousQueue<>();
        //生成
        new Thread(()->{
            try {
                System.out.println("1");
                blockingQueue.put("1");
                System.out.println("2");
                blockingQueue.put("2");
                System.out.println("3");
                blockingQueue.put("3");
            }catch (Exception e){
                e.printStackTrace();
            }
        },"name").start();
        //消费
        new Thread(()->{
            try{
              try {
                  TimeUnit.SECONDS.sleep(5);
              }catch (Exception e){
                  e.printStackTrace();
              }
                System.out.println(Thread.currentThread().getName()+""+ blockingQueue.take());
                try {
                    TimeUnit.SECONDS.sleep(5);
                }catch (Exception e){
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+""+ blockingQueue.take());
                try {
                    TimeUnit.SECONDS.sleep(5);
                }catch (Exception e){
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+""+ blockingQueue.take());
            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }
}
